﻿using NPOI.XWPF.UserModel;

namespace Yz.Manage.Controllers
{
    public class NPOIWordController : BaseController
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public NPOIWordController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }
        public void Index()
        {
            string filePath = _webHostEnvironment.WebRootPath + "/File2023/a.docx";
            using (FileStream fs = System.IO.File.OpenRead(filePath))
            {
                XWPFDocument docx = new XWPFDocument(fs);
                // 获取所有内容不为空的段落
                var pragraphList = docx.Paragraphs.Where(p => p.Text != "").ToList();
                foreach (var paragraph in pragraphList)
                {
                    ///段落文本
                    string paragraphText = paragraph.ParagraphText;
                    ///首行缩进（-1：未进行首行缩进）
                    int intFirstLine = paragraph.FirstLineIndent;
                    ///文本格式（1：居左，2：居中，3：居右）
                    int fontAlignment = paragraph.FontAlignment;
                    ///垂直对齐TextAlignment 
                    TextAlignment verticalAlignment = paragraph.VerticalAlignment;
                    ///空线规则 LineSpacingRule 
                    LineSpacingRule spacingLineRule = paragraph.SpacingLineRule;
                    ///文本内容
                    string textContent = paragraph.Text;
                    ///BodyType   段落类型
                    BodyType partType = paragraph.PartType;
                    ///左缩进
                    int IndentFromLeft = paragraph.IndentFromLeft;
                    ///右缩进
                    int indentFromRight = paragraph.IndentFromRight;
                    ///缩进悬挂
                    int indentationHanging = paragraph.IndentationHanging;
                    ///段落对齐 
                    ParagraphAlignment paragraphAlignment = paragraph.Alignment;


                    //内容分割（针对不同内容）
                    IList<XWPFRun> runs = paragraph.Runs;
                    foreach (var run in runs)
                    {
                        ///字体
                        string fontFamily = run.FontFamily;
                        string fontName = run.FontName;
                        //字号
                        double fontSize = run.FontSize;
                        ///文字加粗
                        bool isBold = run.IsBold;
                        ///是否大写
                        bool isCapitalized = run.IsCapitalized;
                        ///是否斜体
                        bool isItalic = run.IsItalic;
                        ///
                        bool isDoubleStrikeThrough = run.IsDoubleStrikeThrough;
                        ///是否浮雕
                        bool isEmbossed = run.IsEmbossed;
                        ///小写
                        bool isSmallCaps = run.IsSmallCaps;



                    }
                }

            }

        }
    }
}
